<template>
  <el-card>
    <el-form
      :inline="true"
      :model="serchData"
      ref="serchRef"
      class="demo-form-inline"
      size="mini"
    >
      <el-form-item label="订单号" prop="orderNo">
        <el-input v-model="serchData.orderNo" placeholder="订单号"></el-input>
      </el-form-item>
      <el-form-item label="收货人" prop="consignee">
        <el-input v-model="serchData.consignee" placeholder="收货人"></el-input>
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input v-model="serchData.phone" placeholder="手机号"></el-input>
      </el-form-item>
      <el-form-item label="订单状态" prop="orderState">
        <el-select v-model="serchData.orderState" placeholder="订单状态">
          <el-option label="已受理" value="已受理"></el-option>
          <el-option label="派送中" value="派送中"></el-option>
          <el-option label="已完成" value="已完成"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="选择时间" prop="date">
        <el-date-picker
          v-model="serchData.date"
          type="datetimerange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">查询</el-button>
        <el-button type="defalut" @click="resetSubmit">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table :data="tableData" border style="width: 100%">
      <el-table-column fixed prop="orderNo" label="订单号" width="100">
      </el-table-column>
      <el-table-column label="下单时间" width="180">
        <template v-slot="{ row }">
          {{ row.orderTime | formatDate }}
        </template>
      </el-table-column>
      <el-table-column prop="phone" label="手机号" width="120">
      </el-table-column>
      <el-table-column prop="consignee" label="收货人" width="120">
      </el-table-column>
      <el-table-column prop="deliverAddress" label="配送地址" width="100">
      </el-table-column>
      <el-table-column label="送达时间" width="180">
        <template v-slot="{ row }">
          {{ row.deliveryTime | formatDate }}
        </template>
      </el-table-column>
      <el-table-column prop="remarks" label="用户备注" width="120">
      </el-table-column>
      <el-table-column label="订单金额" width="120">
        <template v-slot="{ row }">
          {{ row.orderAmount | formatMoney }}
        </template>
      </el-table-column>
      <el-table-column prop="orderState" label="订单状态" width="120">
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="100">
        <template v-slot="{ row }">
          <el-button type="text" size="small" @click="search(row.id)"
            >查看</el-button
          >
          <el-button
            type="text"
            size="small"
            @click="
              $router.push({ path: '/order/edit', query: { id: row.id } })
            "
            >编辑</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[2, 5, 10, 15]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>

    <el-dialog title="提示" :visible.sync="dialogVisible" width="50%">
      <ul>
        <li>
          <strong>订单号：</strong><span>{{ orderdata.orderNo }}</span>
        </li>
        <li>
          <strong>收货人：</strong><span>{{ orderdata.consignee }}</span>
        </li>
        <li>
          <strong>电话：</strong><span>{{ orderdata.phone }}</span>
        </li>
        <li>
          <strong>下单日期：</strong>
          <span>{{ orderdata.orderTime | formatDate }}</span>
        </li>
        <li>
          <strong>送达时间：</strong>
          <span>{{ orderdata.deliveryTime | formatDate }}</span>
        </li>
        <li>
          <strong>用户备注：</strong><span>{{ orderdata.remarks }}</span>
        </li>
        <li>
          <strong>订单金额：</strong><span>{{ orderdata.orderAmount }}</span>
        </li>
        <li>
          <strong>订单状态：</strong><span>{{ orderdata.orderState }}</span>
        </li>
      </ul>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogVisible = false"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </el-card>
</template>

<script>
import { getOrderList, getOrderdetail } from '../../api/order.js'

export default {
  data() {
    return {
      serchData: {
        orderNo: '',
        consignee: '',
        phone: '',
        orderState: '',
        date: ''
      },
      currentPage: 1,
      pageSize: 5,
      total: 0,
      tableData: [],
      dialogVisible: false,
      orderdata: ''
    }
  },
  created() {
    this.getOrder()
  },
  methods: {
    async getOrder() {
      if (this.tableData.length === 1 && this.currentPage !== 1) {
        this.currentPage--
      }
      const { data, total } = await getOrderList({
        orderNo: this.serchData.orderNo,
        consignee: this.serchData.consignee,
        phone: this.serchData.phone,
        orderState: this.serchData.orderState,
        date: this.serchData.date,
        currentPage: this.currentPage,
        pageSize: this.pageSize
      })
      this.tableData = data
      this.total = total
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.getOrder()
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.getOrder()
    },
    onSubmit() {
      this.currentPage = 1
      this.getOrder()
    },
    resetSubmit() {
      this.$refs.serchRef.resetFields()
      this.serchData.date = ''
      this.currentPage = 1
      this.getOrder()
    },
    async search(id) {
      this.dialogVisible = true
      const { data } = await getOrderdetail({
        id
      })
      this.orderdata = data
    }
  }
}
</script>

<style lang="less" scoped>
.el-pagination {
  margin-top: 20px;
}
li {
  margin-top: 10px;
}
</style>
